﻿using System;
using System.Collections.Generic;
using System.Linq;
using Luna.Data.Project.Mappers;

namespace Luna.Data.Project
{
    public class ProjectDataProvider : LunaDataProvider, IProjectDataProvider
    {
        public ProjectDataProvider()
        {
            DefineTypeCreation<IProjectData, ProjectData>();
            DefineTypeCreation<IProjectTaskData, ProjectTaskData>();
        }

        #region Mappers
        public ProjectDataMapper ProjectMapper { get; set; }

        public ProjectTaskMapper ProjectTaskMapper { get; set; }
        #endregion

        #region Projects
        public IEnumerable<IProjectData> GetAllProjects()
        {
            return WrapperTransactional(T => ProjectMapper.GetAll(T));
        }

        public IProjectData GetProject(Guid P_ProjectID)
        {
            return WrapperTransactional(T => ProjectMapper.Get(T, P_ProjectID));
        }

        public void SaveProject(IProjectData P_Project, IEnumerable<IProjectTaskData> P_Tasks)
        {
            WrapperTransactional(T =>
            {
                ProjectMapper.Save(T, P_Project);

                foreach (var L_Task in ProjectTaskMapper.GetAllByProject(T, P_Project.PK))
                {
                    if (!P_Tasks.Any(x => x.PK == L_Task.PK))
                    {
                        ProjectTaskMapper.Delete(T, L_Task.PK);
                    }
                }

                foreach (var L_Task in P_Tasks)
                {
                    ProjectTaskMapper.Save(T, L_Task);
                }
            });
        }

        public void DeleteProject(Guid P_ProjectID)
        {
            WrapperTransactional(T =>
            {
                foreach (var L_Task in ProjectTaskMapper.GetAllByProject(T, P_ProjectID))
                {
                    ProjectTaskMapper.Delete(T, L_Task.PK);
                }

                ProjectMapper.Delete(T, P_ProjectID);
            });
        }
        #endregion

        #region Metadata
        private const string _ID = "{9D257538-7F7A-4E05-BB84-7E887360D2E7}";

        private const string _NAME = "Luna project data provider";

        public override Guid ID
        {
            get { return new Guid(_ID); }
        }

        public override string Name
        {
            get { return _NAME; }
        }
        #endregion

        #region Tasks
        public IEnumerable<IProjectTaskData> GetAllTasks()
        {
            return WrapperTransactional(T => ProjectTaskMapper.GetAll(T));
        }

        public IEnumerable<IProjectTaskData> GetAllTasksByProject(Guid P_ProjectID)
        {
            return WrapperTransactional(T => ProjectTaskMapper.GetAllByProject(T, P_ProjectID));
        }

        public IProjectTaskData GetTask(Guid P_TaskID)
        {
            return WrapperTransactional(T => ProjectTaskMapper.Get(T, P_TaskID));
        }

        public void SaveTask(IProjectTaskData P_Task)
        {
            WrapperTransactional(T => ProjectTaskMapper.Save(T, P_Task));
        }

        public void DeleteTask(Guid P_TaskID)
        {
            WrapperTransactional(T => ProjectTaskMapper.Delete(T, P_TaskID));
        }
        #endregion
    }
}
